home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / cvtbmp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-09  |  17.7 KB  |  647 lines

  1. /*
  2.  * rdbmp.c
  3.  *
  4.  * Copyright (C) 1994-1996, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This code contributed by James Arthur Boucher.
  9.  * Modified by Peter Zingg, IMSI.
  10.  * Exports a function, DIBtoJPEG, that converts a DIB (8, 16, or 24 bits per pixel) in
  11.  * memory.
  12.  */
  13.  
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "cdjpeg.h"        /* Common decls for cjpeg/djpeg applications */
  17.  
  18. #ifdef BMP_SUPPORTED
  19.  
  20. /* Macros to deal with unsigned chars as efficiently as compiler allows */
  21.  
  22. #ifdef HAVE_UNSIGNED_CHAR
  23. typedef unsigned char U_CHAR;
  24. #define UCH(x)    ((int) (x))
  25. #else /* !HAVE_UNSIGNED_CHAR */
  26. #ifdef CHAR_IS_UNSIGNED
  27. typedef char U_CHAR;
  28. #define UCH(x)    ((int) (x))
  29. #else
  30. typedef char U_CHAR;
  31. #define UCH(x)    ((int) (x) & 0xFF)
  32. #endif
  33. #endif /* HAVE_UNSIGNED_CHAR */
  34.  
  35.  
  36. /*
  37. typedef struct tagBITMAPINFOHEADER{ // bmih  
  38.    DWORD  biSize; 
  39.    LONG   biWidth; 
  40.    LONG   biHeight; 
  41.    WORD   biPlanes; 
  42.    WORD   biBitCount 
  43.    DWORD  biCompression; 
  44.    DWORD  biSizeImage; 
  45.    LONG   biXPelsPerMeter; 
  46.    LONG   biYPelsPerMeter; 
  47.    DWORD  biClrUsed; 
  48.    DWORD  biClrImportant; 
  49. } BITMAPINFOHEADER; 
  50. */
  51.  
  52. /* Private version of data source object */
  53.  
  54. typedef struct _bmp_source_struct * bmp_source_ptr;
  55.  
  56. typedef struct _bmp_source_struct {
  57.   struct cjpeg_source_struct pub; /* public fields */
  58.  
  59.   j_compress_ptr cinfo;        /* back link saves passing separate parm */
  60.   HGLOBAL global_handle;    /* Windows DIB handle */
  61.   LPBYTE colormap;          /* Start of colormap data */
  62.   int colormap_entry_size;    /* Colormap entry size (3 for RGBTRIPLE, 4 for RGBQUAD) */
  63.   int colors;               /* Number of colors in colormap */
  64.   LPBYTE bitmap_data;        /* Start of bitmap bits if handle is locked */
  65.   JDIMENSION row_width;        /* Physical width of scanlines in file */
  66.   JDIMENSION source_row;    /* Current source row number */
  67. } bmp_source_struct;
  68.  
  69.  
  70. LOCAL(int)
  71. bitmap_colors(LPBITMAPINFOHEADER lpbi)
  72. {
  73.     int                 bits;
  74.     LPBITMAPCOREHEADER  lpbc = (LPBITMAPCOREHEADER)lpbi;
  75.  
  76.     /*  With the BITMAPINFO format headers, the size of the palette
  77.     *  is in biClrUsed, whereas in the BITMAPCORE - style headers, it
  78.     *  is dependent on the bits per pixel ( = 2 raised to the power of
  79.     *  bits/pixel).
  80.     */
  81.     if (lpbi->biSize != sizeof(BITMAPCOREHEADER))
  82.     {
  83.         if (lpbi->biClrUsed != 0)
  84.             return (int)lpbi->biClrUsed;
  85.         bits = lpbi->biBitCount;
  86.     }
  87.     else
  88.     {
  89.         bits = lpbc->bcBitCount;
  90.     }
  91.  
  92.     switch (bits)
  93.     {
  94.     case 1:
  95.         return 2;
  96.     case 4:
  97.         return 16;
  98.     case 8:
  99.         return 256;
  100.     default:
  101.         break;
  102.     }
  103.     /* A 24 bitcount DIB has no color table */
  104.     return 0;
  105. }
  106.  
  107. LOCAL(int)
  108. bitmap_colormap_size(LPBITMAPINFOHEADER lpbi)
  109. {
  110.     int colors = bitmap_colors(lpbi);
  111.  
  112.     if (lpbi->biSize == sizeof(BITMAPCOREHEADER))
  113.         return colors * sizeof(RGBTRIPLE);
  114.     return colors * sizeof(RGBQUAD);
  115. }
  116.  
  117. /*
  118.  * Return the offset into the bitmap for the specified row.
  119.  */
  120.  
  121. LOCAL(JSAMPROW)
  122. get_bitmap_row_start(bmp_source_ptr source)
  123. {
  124.     JDIMENSION offset = source->source_row * source->row_width;
  125.     return (JSAMPROW)(source->bitmap_data + offset);
  126. }
  127.  
  128. LOCAL(JSAMPROW)
  129. get_bitmap_color(bmp_source_ptr source, int color_index)
  130. {
  131.     JDIMENSION offset = color_index * source->colormap_entry_size;
  132.     return (JSAMPROW)(source->colormap + offset);
  133. }
  134.  
  135. /*
  136.  * Read one row of pixels.
  137.  * The image has been read into the bitmap_data array, but is otherwise
  138.  * unprocessed.  We must read it out in top-to-bottom row order, and if
  139.  * it is an 8-bit image, we must expand colormapped pixels to 24bit format.
  140.  */
  141.  
  142. METHODDEF(JDIMENSION)
  143. get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  144. /* This version is for reading 8-bit colormap indexes */
  145. {
  146.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  147.   register int color_index;
  148.   register JSAMPROW color;
  149.   register JSAMPROW inptr, outptr;
  150.   register JDIMENSION col;
  151.  
  152.   /* Fetch next row from virtual array */
  153.   source->source_row--;
  154.  
  155.   /* Expand the colormap indexes to real data */
  156.   inptr = get_bitmap_row_start(source);
  157.   outptr = source->pub.buffer[0];
  158.   for (col = cinfo->image_width; col > 0; col--) {
  159.     color_index = GETJSAMPLE(*inptr++);
  160.     color = get_bitmap_color(source, color_index);
  161.     *outptr++ = color[2];    /* can omit GETJSAMPLE() safely */
  162.     *outptr++ = color[1];
  163.     *outptr++ = color[0];
  164.   }
  165.  
  166.   return 1;
  167. }
  168.  
  169. #pragma pack(1)
  170. union color16 {
  171.   struct { 
  172.       BYTE lo;
  173.       BYTE hi; 
  174.   } w;
  175.   struct { 
  176.       unsigned int r: 5; 
  177.       unsigned int g: 5;
  178.       unsigned int b: 5;
  179.       unsigned int d: 1;
  180.   } rgb;
  181. };
  182. #pragma pack()
  183.  
  184. METHODDEF(JDIMENSION)
  185. get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  186. /* This version is for reading 16-bit pixels */
  187. {
  188.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  189.   register JSAMPROW inptr, outptr;
  190.   register JDIMENSION col;
  191.   register union color16 color;
  192.  
  193.   /* Fetch next row from virtual array */
  194.   source->source_row--;
  195.  
  196.   /* Transfer data.  Note source values are in BGR order
  197.    * (even though Microsoft's own documents say the opposite).
  198.    */
  199.   inptr = get_bitmap_row_start(source);
  200.   outptr = source->pub.buffer[0];
  201.   for (col = cinfo->image_width; col > 0; col--) {
  202.     color.w.lo = GETJSAMPLE(*inptr++);
  203.     color.w.hi = GETJSAMPLE(*inptr++);
  204.     /* shift 5-bit RGB values to get them to 8 bit values */
  205.     outptr[2] = color.rgb.r;    /* can omit GETJSAMPLE() safely */
  206.     if (outptr[2] == 0x1F)
  207.         outptr[2] = 0xFF;
  208.     else
  209.         outptr[2] <<= 3;
  210.     outptr[1] = color.rgb.g;
  211.     if (outptr[1] == 0x1F)
  212.         outptr[1] = 0xFF;
  213.     else
  214.         outptr[1] <<= 3;
  215.     outptr[0] = color.rgb.b;
  216.     if (outptr[0] == 0x1F)
  217.         outptr[0] = 0xFF;
  218.     else
  219.         outptr[0] <<= 3;
  220.     outptr += 3;
  221.   }
  222.  
  223.   return 1;
  224. }
  225.  
  226.  
  227. METHODDEF(JDIMENSION)
  228. get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  229. /* This version is for reading 24-bit pixels */
  230. {
  231.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  232.   register JSAMPROW inptr, outptr;
  233.   register JDIMENSION col;
  234.  
  235.   /* Fetch next row from virtual array */
  236.   source->source_row--;
  237.  
  238.   /* Transfer data.  Note source values are in BGR order
  239.    * (even though Microsoft's own documents say the opposite).
  240.    */
  241.   inptr = get_bitmap_row_start(source);
  242.   outptr = source->pub.buffer[0];
  243.   for (col = cinfo->image_width; col > 0; col--) {
  244.     outptr[2] = *inptr++;    /* can omit GETJSAMPLE() safely */
  245.     outptr[1] = *inptr++;
  246.     outptr[0] = *inptr++;
  247.     outptr += 3;
  248.   }
  249.  
  250.   return 1;
  251. }
  252.  
  253.  
  254. /*
  255.  * Extract information from bitmap header.
  256.  * Set up parameters.
  257.  * Set up buffers and get_pixel_row callback.
  258.  */
  259.  
  260. METHODDEF(void)
  261. start_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  262. {
  263.   int mapentrysize = 0;
  264.   INT32 headerSize;
  265.   INT32 biWidth = 0;        /* initialize to avoid compiler warning */
  266.   INT32 biHeight = 0;
  267.   unsigned int biPlanes;
  268.   int data_precision;
  269.   int bits_per_pixel;    
  270.   int palette_colors;
  271.   INT32 biCompression;
  272.   INT32 biXPelsPerMeter,biYPelsPerMeter;
  273.   JDIMENSION row_width;
  274.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  275.   LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)::GlobalLock(source->global_handle);
  276.   LPBITMAPCOREHEADER lpbc;
  277.  
  278.   if (lpbi == NULL)
  279.       ERREXIT(cinfo, JERR_BMP_NOT);
  280.  
  281.   palette_colors = bitmap_colors(lpbi);
  282.   source->bitmap_data = (LPBYTE)lpbi + lpbi->biSize + bitmap_colormap_size(lpbi);
  283.  
  284.   /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
  285.    * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.
  286.    */
  287.   headerSize = lpbi->biSize;
  288.   if (headerSize < 12 || headerSize > 64)
  289.     ERREXIT(cinfo, JERR_BMP_BADHEADER);
  290.  
  291.   switch ((int) headerSize) {
  292.   case sizeof(BITMAPCOREHEADER):
  293.     lpbc = (LPBITMAPCOREHEADER)lpbi;
  294.     /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
  295.     biWidth = lpbc->bcWidth;
  296.     biHeight = lpbc->bcHeight;
  297.     biPlanes = lpbc->bcPlanes;
  298.     bits_per_pixel = lpbc->bcBitCount;
  299.  
  300.     switch (bits_per_pixel) {
  301.     case 8:            /* colormapped image */
  302.       mapentrysize = 3;        /* OS/2 uses RGBTRIPLE colormap */
  303.       TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, (int) biWidth, (int) biHeight);
  304.       break;
  305.     case 16:
  306.     case 24:            /* RGB image */
  307.       TRACEMS2(cinfo, 1, JTRC_BMP_OS2, (int) biWidth, (int) biHeight);
  308.       break;
  309.     default:
  310.       ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  311.       break;
  312.     }
  313.     if (biPlanes != 1)
  314.       ERREXIT(cinfo, JERR_BMP_BADPLANES);
  315.     break;
  316.   case 40:
  317.   case 64:
  318.     /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
  319.     /* or OS/2 2.x header, which has additional fields that we ignore */
  320.     biWidth = lpbi->biWidth;
  321.     biHeight = lpbi->biHeight;
  322.     biPlanes = lpbi->biPlanes;
  323.     bits_per_pixel = lpbi->biBitCount;
  324.     biCompression = lpbi->biCompression;
  325.     biXPelsPerMeter = lpbi->biXPelsPerMeter;
  326.     biYPelsPerMeter = lpbi->biYPelsPerMeter;
  327.     /* biSizeImage, biClrImportant fields are ignored */
  328.  
  329.     switch (bits_per_pixel) {
  330.     case 8:            /* colormapped image */
  331.       mapentrysize = 4;        /* RGBQUAD colormap */
  332.       TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, (int) biWidth, (int) biHeight);
  333.       break;
  334.     case 16:
  335.     case 24:            /* RGB image */
  336.       TRACEMS2(cinfo, 1, JTRC_BMP, (int) biWidth, (int) biHeight);
  337.       break;
  338.     default:
  339.         ERREXIT(cinfo, JERR_BMP_BADDEPTH);
  340.       break;
  341.     }
  342.     if (biPlanes != 1)
  343.       ERREXIT(cinfo, JERR_BMP_BADPLANES);
  344.     if (biCompression != 0)
  345.       ERREXIT(cinfo, JERR_BMP_COMPRESSED);
  346.  
  347.     if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
  348.       /* Set JFIF density parameters from the BMP data */
  349.       cinfo->X_density = (UINT16) (biXPelsPerMeter/100); /* 100 cm per meter */
  350.       cinfo->Y_density = (UINT16) (biYPelsPerMeter/100);
  351.       cinfo->density_unit = 2;    /* dots/cm */
  352.     }
  353.     break;
  354.   default:
  355.     ERREXIT(cinfo, JERR_BMP_BADHEADER);
  356.     break;
  357.   }
  358.  
  359.   /* Read the colormap, if any */
  360.   if (mapentrysize > 0) {
  361.     if (palette_colors <= 0)
  362.       palette_colors = 256;        /* assume it's 256 */
  363.     else if (palette_colors > 256)
  364.       ERREXIT(cinfo, JERR_BMP_BADCMAP);
  365.  
  366.     /* Point to colormap */
  367.     source->colormap = (LPBYTE)lpbi + lpbi->biSize;
  368.     source->colormap_entry_size = mapentrysize;
  369.     source->colors = palette_colors;
  370.   }
  371.  
  372.   /* Compute row width in file, including padding to 4-byte boundary */
  373.   /* Set up to read from the virtual array in top-to-bottom order */
  374.   switch (bits_per_pixel)
  375.   {
  376.   case 8:
  377.     source->pub.get_pixel_rows = get_8bit_row;
  378.     data_precision = 8;
  379.     row_width = (JDIMENSION) biWidth; break;
  380.   case 16: 
  381.     source->pub.get_pixel_rows = get_16bit_row;
  382.     data_precision = 8;
  383.     row_width = (JDIMENSION) (biWidth * 2); break;
  384.   case 24:
  385.     source->pub.get_pixel_rows = get_24bit_row;
  386.     data_precision = 8;
  387.     row_width = (JDIMENSION) (biWidth * 3); break;
  388.   }
  389.   while ((row_width & 3) != 0) row_width++;
  390.  
  391.   cinfo->in_color_space = JCS_RGB;
  392.   cinfo->input_components = 3;
  393.   cinfo->data_precision = data_precision;
  394.   cinfo->image_width = (JDIMENSION) biWidth;
  395.   cinfo->image_height = (JDIMENSION) biHeight;
  396.  
  397.   source->row_width = row_width;
  398.   source->source_row = cinfo->image_height;
  399.  
  400.   /* Allocate one-row buffer for returned data */
  401.   source->pub.buffer = (*cinfo->mem->alloc_sarray)
  402.     ((j_common_ptr) cinfo, JPOOL_IMAGE,
  403.      (JDIMENSION) (biWidth * 3), (JDIMENSION) 1);
  404.   source->pub.buffer_height = 1;
  405. }
  406.  
  407.  
  408. /*
  409.  * Finish up at the end of the file.
  410.  */
  411.  
  412. METHODDEF(void)
  413. finish_input_bmp (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
  414. {
  415.   bmp_source_ptr source = (bmp_source_ptr) sinfo;
  416.   if (source->bitmap_data == NULL)
  417.       return;
  418.  
  419.   /* Done with global memory */
  420.   ::GlobalUnlock(source->global_handle);
  421.  
  422.   /* Invalidate internal pointers */
  423.   source->bitmap_data = NULL;
  424.   source->colormap = NULL;
  425. }
  426.  
  427.  
  428. /*
  429.  * The module selection routine for BMP format input.
  430.  */
  431.  
  432. GLOBAL(cjpeg_source_ptr)
  433. jinit_cvt_bmp (j_compress_ptr cinfo, HGLOBAL hDib)
  434. {
  435.   bmp_source_ptr source;
  436.  
  437.   /* Create module interface object */
  438.   source = (bmp_source_ptr)
  439.       (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  440.                   SIZEOF(bmp_source_struct));
  441.   source->cinfo = cinfo;    /* make back link for subroutines */
  442.   /* Fill in method ptrs, except get_pixel_rows which start_input sets */
  443.   source->pub.start_input = start_input_bmp;
  444.   source->pub.finish_input = finish_input_bmp;
  445.   source->global_handle = hDib;    /* Windows DIB handle */
  446.   source->colormap = NULL;
  447.   source->colormap_entry_size = 0;
  448.   source->colors = 0;
  449.   source->bitmap_data = NULL;
  450.   source->source_row = 0;    /* Current source row number */
  451.   source->row_width = 0;        /* Physical width of scanlines in file */
  452.  
  453.   return (cjpeg_source_ptr) source;
  454. }
  455.  
  456.  
  457. /*
  458.  * The main program.
  459.  */
  460.  
  461. /* Create the add-on message string table. */
  462.  
  463. #define JMESSAGE(code,string)    string ,
  464.  
  465. static const char * const cdjpeg_message_table[] = {
  466. #include "cderror.h"
  467.   NULL
  468. };
  469.  
  470.  
  471. METHODDEF(void)
  472. save_output_message (j_common_ptr cinfo)
  473. {
  474.   /* Create the message */
  475.   char szErrorBuffer[JMSG_LENGTH_MAX];
  476.   (*cinfo->err->format_message) (cinfo, szErrorBuffer);
  477. }
  478.  
  479. class CMyException: public CException
  480. {
  481.     DECLARE_DYNAMIC(CMyException)
  482.  
  483. public:
  484.     char m_szDesc[JMSG_LENGTH_MAX];
  485.  
  486. // Implementation (use AfxThrowOleException to create)
  487. public:
  488.     CMyException() { m_szDesc[0] = '\0'; } 
  489.     virtual BOOL GetErrorMessage(LPTSTR lpszError, UINT nMaxError, PUINT pnHelpContext = NULL)
  490.     {
  491.         if (m_szDesc[0] = '\0' || nMaxError <= 1)
  492.             return FALSE;
  493.  
  494.         _tcsncpy(lpszError, m_szDesc, nMaxError-1);
  495.         lpszError[nMaxError-1] = '\0';
  496.         return TRUE;
  497.     }
  498. };
  499.  
  500. IMPLEMENT_DYNAMIC(CMyException, CException);
  501.  
  502. METHODDEF(void)
  503. my_error_exit (j_common_ptr cinfo)
  504. {
  505.   CMyException* pException = new CMyException;
  506.  
  507.   /* Always display the message. */
  508.   /* We could postpone this until after returning, if we chose. */
  509.   (*cinfo->err->format_message) (cinfo, pException->m_szDesc);
  510.   THROW(pException);
  511. }
  512.  
  513.  
  514. JPEGLIB(int)
  515. DIBToJPEG (HGLOBAL hDib, LPCSTR outfilename)
  516. {
  517.   int nError = -101;
  518.   struct jpeg_compress_struct cinfo;
  519.   struct jpeg_error_mgr jerr;
  520. #ifdef PROGRESS_REPORT
  521.   struct cdjpeg_progress_mgr progress;
  522. #endif
  523.   cjpeg_source_ptr src_mgr = NULL;
  524.   FILE * output_file = NULL;
  525.   JDIMENSION num_scanlines;
  526.  
  527.   const char* progname = "JpegLib";        /* in case C library doesn't provide it */
  528.  
  529.   /* Initialize the JPEG compression object with default error handling. */
  530.   cinfo.err = jpeg_std_error(&jerr);
  531.   jerr.output_message = save_output_message;
  532.  
  533.   jpeg_create_compress(&cinfo);
  534.   /* Add some application-specific error messages (from cderror.h) */
  535.   jerr.addon_message_table = cdjpeg_message_table;
  536.   jerr.first_addon_message = JMSG_FIRSTADDONCODE;
  537.   jerr.last_addon_message = JMSG_LASTADDONCODE;
  538.  
  539.   /* Now safe to enable signal catcher. */
  540. #ifdef NEED_SIGNAL_CATCHER
  541.   enable_signal_catcher((j_common_ptr) &cinfo);
  542. #endif
  543.  
  544.   /* Initialize JPEG parameters.
  545.    * Much of this may be overridden later.
  546.    * In particular, we don't yet know the input file's color space,
  547.    * but we need to provide some value for jpeg_set_defaults() to work.
  548.    */
  549.  
  550.   cinfo.in_color_space = JCS_RGB; /* arbitrary guess */
  551.   jpeg_set_defaults(&cinfo);
  552.  
  553.   /* Scan command line to find file names.
  554.    * It is convenient to use just one switch-parsing routine, but the switch
  555.    * values read here are ignored; we will rescan the switches after opening
  556.    * the input file.
  557.    */
  558.  
  559. //  file_index = parse_switches(&cinfo, argc, argv, 0, FALSE);
  560.  
  561.   /* Open the output file. */
  562.     if ((output_file = fopen(outfilename, WRITE_BINARY)) == NULL) {
  563.       fprintf(stderr, "%s: can't open %s\n", progname, outfilename);
  564.       // exit(EXIT_FAILURE);
  565.       nError = -102;
  566.       return nError;
  567.     }
  568.  
  569.   TRY
  570.   {
  571. #ifdef PROGRESS_REPORT
  572.       start_progress_monitor((j_common_ptr) &cinfo, &progress);
  573. #endif
  574.  
  575.       /* Figure out the input file format, and set up to read it. */
  576.  
  577.       
  578.       src_mgr = jinit_cvt_bmp(&cinfo, hDib);
  579.         bmp_source_ptr source = (bmp_source_ptr) src_mgr;
  580.         LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)::GlobalLock(source->global_handle);
  581.         if (lpbi != NULL)
  582.         {
  583.             // do nothing  if true color
  584.             int bits_per_pixel = lpbi->biBitCount;
  585.             if (bits_per_pixel > 24) // do nothing  if true color
  586.             {
  587.                 fclose(output_file);
  588.                 bool bSuc = DeleteFile(outfilename);
  589.                 nError = -102;
  590.                 return -555;
  591.             }
  592.       }
  593.  
  594.       /* Read the input file header to obtain file size & colorspace. */
  595.       (*src_mgr->start_input) (&cinfo, src_mgr);
  596.  
  597.       /* Now that we know input colorspace, fix colorspace-dependent defaults */
  598.       jpeg_default_colorspace(&cinfo);
  599.  
  600.       /* Adjust default compression parameters by re-parsing the options */
  601.     //  file_index = parse_switches(&cinfo, argc, argv, 0, TRUE);
  602.  
  603.       /* Specify data destination for compression */
  604.       jpeg_stdio_dest(&cinfo, output_file);
  605.  
  606.       /* Start compressor */
  607.       jpeg_start_compress(&cinfo, TRUE);
  608.  
  609.       /* Process data */
  610.       while (cinfo.next_scanline < cinfo.image_height) {
  611.         num_scanlines = (*src_mgr->get_pixel_rows) (&cinfo, src_mgr);
  612.         (void) jpeg_write_scanlines(&cinfo, src_mgr->buffer, num_scanlines);
  613.       }
  614.           /* Finish compression and release memory */
  615.       (*src_mgr->finish_input) (&cinfo, src_mgr);
  616.       jpeg_finish_compress(&cinfo);
  617.       jpeg_destroy_compress(&cinfo);
  618.  
  619.       /* Close files, if we opened them */
  620.          fclose(output_file);
  621.  
  622. #ifdef PROGRESS_REPORT
  623.   end_progress_monitor((j_common_ptr) &cinfo);
  624. #endif
  625.  
  626.       nError = 0;
  627.   }
  628.   CATCH_ALL(e)
  629.   {
  630.       if (src_mgr != NULL)
  631.         (*src_mgr->finish_input) (&cinfo, src_mgr);
  632.  
  633.       jpeg_destroy_compress(&cinfo);
  634.  
  635.       /* Close files, if we opened them */
  636.          fclose(output_file);
  637.       nError = -103;
  638.   }
  639.   END_CATCH_ALL
  640.  
  641.   /* All done. */
  642.   // exit(jerr.num_warnings ? EXIT_WARNING : EXIT_SUCCESS);
  643.   return nError;        
  644. }
  645.  
  646. #endif /* BMP_SUPPORTED */
  647.